package service

import (
	"context"
	"mall-portalv2/internal/model"
)

func (s *Service) QueryOmsOrderWhereIdNoUser(ctx context.Context, id int64) (model.OmsOrder, error) {
	result, err := s.dao.QueryOmsOrderWhereIdNoUser(ctx, id)
	if err != nil {
		return model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) QueryOmsOrderWhereId(ctx context.Context, id int64, memberId int64) (model.OmsOrder, error) {
	result, err := s.dao.QueryOmsOrderWhereId(ctx, id, memberId)
	if err != nil {
		return model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) QueryOmsOrderList(ctx context.Context, memberId int64, status, pageNum, pageSize int) ([]model.OmsOrder, error) {

	var (
		result []model.OmsOrder
		err    error
		limit  = pageSize
		offset = (pageNum - 1) * pageSize
	)

	if status == -1 {
		result, err = s.dao.QueryOmsOrderListAll(ctx, memberId, limit, offset)
		if err != nil {
			return []model.OmsOrder{}, err
		}
	} else {
		result, err = s.dao.QueryOmsOrderList(ctx, memberId, status, limit, offset)
		if err != nil {
			return []model.OmsOrder{}, err
		}
	}
	return result, err
}

func (s *Service) QueryOmsOrderListAll(ctx context.Context, memberId int64, MemberCom int) ([]model.OmsOrder, error) {

	var (
		result []model.OmsOrder
		err    error
	)

	result, err = s.dao.QueryOmsOrderListAllNotLimit(ctx, memberId, MemberCom)
	if err != nil {
		return []model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) QueryOmsOrderListAllAndNoMemberCom(ctx context.Context, memberId int64) ([]model.OmsOrder, error) {

	var (
		result []model.OmsOrder
		err    error
	)

	result, err = s.dao.QueryOmsOrderListAllNotLimitAndNoMemCom(ctx, memberId)
	if err != nil {
		return []model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) QueryOmsOrderListCount(ctx context.Context, memberId int64, status, MemberCom int) (int64, error) {

	var (
		result int64
		err    error
	)

	if status == -1 {
		result, err = s.dao.QueryOmsOrderListAllCount(ctx, memberId, MemberCom)
		if err != nil {
			return 0, err
		}
	} else {
		result, err = s.dao.QueryOmsOrderListCount(ctx, memberId, status, MemberCom)
		if err != nil {
			return 0, err
		}
	}
	return result, err
}

func (s *Service) CreateOrder(ctx context.Context, insertData model.OmsOrder) (model.OmsOrder, error) {
	result, err := s.dao.CreateOrder(ctx, insertData)
	if err != nil {
		return model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) UpdateOrder(ctx context.Context, ID int64, upadtes map[string]interface{}) error {
	if err := s.dao.UpdateOrder(ctx, ID, upadtes); err != nil {
		return err
	}
	return nil
}

func (s *Service) UpdateOrderWhereOrderSn(ctx context.Context, OrderSn string, upadtes map[string]interface{}) error {
	if err := s.dao.UpdateOrderWhereOrderSn(ctx, OrderSn, upadtes); err != nil {
		return err
	}
	return nil
}

func (s *Service) UpdateOrderWhereIds(ctx context.Context, Ids []int64, upadtes map[string]interface{}, UID int64) error {
	if err := s.dao.UpdateOrderWhereIds(ctx, Ids, upadtes, UID); err != nil {
		return err
	}
	return nil
}

func (s *Service) QueryOmsOrderWhereOrderSn(ctx context.Context, OrderSn string) (model.OmsOrder, error) {
	result, err := s.dao.QueryOmsOrderWhereOrderSn(ctx, OrderSn)
	if err != nil {
		return model.OmsOrder{}, err
	}
	return result, err
}

func (s *Service) UnLockOmsOrder(ctx context.Context, orderId int64) error {
	if err := s.dao.UnLockOmsOrder(ctx, orderId); err != nil {
		return err
	}
	return nil
}

func (s *Service) UnLockOmsOrderFun2(ctx context.Context, ProductSkuId, SkuLockStock, CrowdFundingSkuId, CftLockThaliCount, num int64) error {
	if err := s.dao.UnLockOmsOrderFun2(ctx, ProductSkuId, SkuLockStock, CrowdFundingSkuId, CftLockThaliCount, num); err != nil {
		return err
	}
	return nil
}

func (s *Service) LockOmsOrder(ctx context.Context, ProductSkuId, SkuLockStock, CrowdFundingSkuId, CftLockThaliCount, num int64) error {
	if err := s.dao.LockOmsOrder(ctx, ProductSkuId, SkuLockStock, CrowdFundingSkuId, CftLockThaliCount, num); err != nil {
		return err
	}
	return nil
}
